#include "TestLayout.h"
#include<QHBoxLayout>
#include<QVBoxLayout>
#include<QGridLayout>
#include<QFormLayout>
#include<QStackedLayout>
#include<QSpacerItem>
#include<QLayoutItem>
#include<QPushButton>
#include<QLabel>
#include<QTextEdit>
#include<QLineEdit>

TestLayout::TestLayout(QWidget *parent) : QWidget(parent)
{
    //1.​​QHBoxLayout​ 水平布局使用
    QHBoxLayout *hLayout =new QHBoxLayout();
    QPushButton *btn1 = new QPushButton("Button h1");
    QPushButton *btn2 = new QPushButton("Button h2");
    hLayout->addWidget(btn1);
    hLayout->addWidget(btn2);


    //2.​​QVBoxLayout​​垂直排列子部件
    QVBoxLayout *vLayout = new QVBoxLayout();
    QLabel *label_v =new QLabel("label",this);
    QTextEdit *textEdit_v =new QTextEdit("textEdit",this);

    vLayout->addLayout(hLayout);
    vLayout->addWidget(label_v);
    vLayout->addWidget(textEdit_v);
    vLayout->setContentsMargins(0,0,0,0);
    this->setLayout(vLayout);

    //3.QGridLayout网格布局（行和列）
    QGridLayout *gridLayout = new QGridLayout();
    QPushButton *btn_grid_A =new QPushButton("btn_grid_A",this);
    QPushButton *btn_grid_B =new QPushButton("btn_grid_B",this);
    QPushButton *btn_grid_C =new QPushButton("btn_grid_C",this);
    gridLayout->addWidget(btn_grid_A, 0, 0); // 第0行，第0列
    gridLayout->addWidget(btn_grid_B, 0, 1); // 第0行，第1列
    gridLayout->addWidget(btn_grid_C, 1, 0, 1, 2); // 跨1行2列
    vLayout->addLayout(gridLayout);

    //4 QFormLayout​​ 表单布局（标签 + 输入框）
    QFormLayout *formLayout = new QFormLayout();
    QLineEdit *LineEdit_name =new QLineEdit(this);
    QLineEdit *LineEdit_email =new QLineEdit(this);
    formLayout->addRow("Name:", LineEdit_name);
    formLayout->addRow("Email:", LineEdit_email);
    vLayout->addLayout(formLayout);

    //5 ​​QStackedLayout​​ 堆叠布局（同一区域显示不同部件）
    QStackedLayout *stackedLayout = new QStackedLayout;
    QWidget *page1 =new QWidget(this);
    QLabel *label_page1 =new QLabel("label page1",page1);
    QVBoxLayout *vlayout_page1 = new QVBoxLayout(page1);
    vlayout_page1->addWidget(label_page1);

    QWidget *page2 =new QWidget(this);
    QLabel *label_page2 =new QLabel("label page2",page2);
    QVBoxLayout *vlayout_page2 = new QVBoxLayout(page2);
    vlayout_page2->addWidget(label_page2);

    stackedLayout->addWidget(page1);
    stackedLayout->addWidget(page2);
    stackedLayout->setCurrentIndex(1); // 显示page2
    vLayout->addLayout(stackedLayout);


    //6 ​​边距与间距​​
    vLayout->setContentsMargins(10, 10, 10, 10); // 左,上,右,下
    vLayout->setSpacing(5); // 部件间距

    //7 拉伸因子​​
    hLayout->addWidget(btn1, 1); // 拉伸因子1
    hLayout->addWidget(btn2, 2); // 拉伸因子2（占用2倍空间）

    //8 对齐方式​​Align
    QHBoxLayout *hLayout_Align =new QHBoxLayout();
    QPushButton *btn_Align = new QPushButton("btn__Align");
    hLayout_Align->addWidget(btn_Align);
    hLayout_Align->setAlignment(btn_Align, Qt::AlignCenter); // 居中对齐
    vLayout->addLayout(hLayout_Align);

    //9 ​​响应式设计​​,使用拉伸因子（addStretch()）创建弹性空间：
    QHBoxLayout *hLayout_Stretch =new QHBoxLayout();
    QPushButton *btn_Stretch = new QPushButton("btn_Stretch");
    hLayout_Stretch->addStretch(1); // 左侧弹性空间
    hLayout_Stretch->addWidget(btn_Stretch);
    hLayout_Stretch->addStretch(1); // 右侧弹性空间
    vLayout->addLayout(hLayout_Stretch);

}
